import { requestClient } from '#/api/request';

export interface CascaderOptionModel {
  label: string;
  value: string;
  isLeaf: boolean;
  children: CascaderOptionModel[];
}

export async function loadRegionByParentIdApi(parentId: string = '0') {
  return requestClient.post('/region/page', {
    parentId,
    searchCount: false,
    current: 1,
    size: 100,
    orders: [{ column: 'orderNo' }],
  });
}

export async function loadRegionByParentId(
  parentId: string,
): Promise<CascaderOptionModel[]> {
  const options: CascaderOptionModel[] = [];
  const result = await loadRegionByParentIdApi(parentId);
  result.records.forEach((element: any) => {
    options.push({
      label: element.regionName,
      value: element.regionId,
      isLeaf: element.isLeaf,
      children: [],
    });
  });
  return options;
}
